﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
  public  class PhieuSuDungDichVuDAO
    {
      public List<PhieuSuDungDichVuDTO> DanhSachPhieuSuDungDichVu()
      {
          List<PhieuSuDungDichVuDTO> kq = new List<PhieuSuDungDichVuDTO>();
          string str = "select*from PhieuSuDungDichVu";
          DataProvider dp = new DataProvider();
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(str);
          if (dr.HasRows)
          {
              while (dr.Read())
              {
                  PhieuSuDungDichVuDTO psd = new PhieuSuDungDichVuDTO();
                  psd.MaPhieuSuDungDichVu = dr["MaPhieuSuDungDichVu"].ToString();
                  psd.NgayLap = DateTime.Parse(dr["NgayLap"].ToString());
                  psd.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
                  psd.PSDDV_ID = int.Parse(dr["PSDDV_ID"].ToString());
                  psd.TenDangNhap = dr["TenDangNhap"].ToString();
                  kq.Add(psd);
              }
          }
          return kq;
      }
      public PhieuSuDungDichVuDTO LayPhieuSuDungDichVuByID(int pSDDV_ID)
      {
          PhieuSuDungDichVuDTO psd = new PhieuSuDungDichVuDTO();
          string str = "select*from pSDDV_ID where PSDDV_ID=" + pSDDV_ID;
          DataProvider dp = new DataProvider();
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(str);
          dr.Read();
          psd.MaPhieuSuDungDichVu = dr["MaPhieuSuDungDichVu"].ToString();
          psd.NgayLap = DateTime.Parse(dr["NgayLap"].ToString());
          psd.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
          psd.PSDDV_ID = int.Parse(dr["PSDDV_ID"].ToString());
          psd.TenDangNhap = dr["TenDangNhap"].ToString();
          dp.Disconnect();
          return psd;
      }
      public PhieuSuDungDichVuDTO LayPhieuSuDungDichVuByMaPhieuSuDungDichVu(string maPhieuSuDungDichVu)
      {
          PhieuSuDungDichVuDTO psd = new PhieuSuDungDichVuDTO();
          string str = "select*from pSDDV_ID where MaPhieuSuDungDichVu='" +maPhieuSuDungDichVu+"'";
          DataProvider dp = new DataProvider();
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(str);
          dr.Read();
          psd.MaPhieuSuDungDichVu = dr["MaPhieuSuDungDichVu"].ToString();
          psd.NgayLap = DateTime.Parse(dr["NgayLap"].ToString());
          psd.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
          psd.PSDDV_ID = int.Parse(dr["PSDDV_ID"].ToString());
          psd.TenDangNhap = dr["TenDangNhap"].ToString();
          dp.Disconnect();
          return psd;
      }
      public Boolean ThemPhieuSuDungDichVu(PhieuSuDungDichVuDTO psd)
      {
          Boolean kq = false;
          string str = "insert into PhieuSuDungDichVu(MaPhieuSuDungDichVu,NgayLap,PDP_ID,TenDangNhap)";
          str += "values('" + psd.MaPhieuSuDungDichVu + "','" + psd.NgayLap + "'," + psd.PDP_ID + ",'" + psd.TenDangNhap + "')";
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(str);
          if (i != 0)
              kq = true;
          return kq;
      }
      public Boolean CapNhatPhieuSuDungDichVu(PhieuSuDungDichVuDTO psd)
      {
          Boolean kq = false;
          string str = "update PhieuSuDungDichVu set MaPhieuSuDungDichVu='"+psd.MaPhieuSuDungDichVu+"' and NgayLap='"+psd.NgayLap+"' and PDP_ID="+psd.PDP_ID+" and TenDangNhap='"+psd.TenDangNhap+"'";
          str += " where PSDDV_ID=" + psd.PSDDV_ID;
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(str);
          if (i != 0)
              kq = true;
          return kq;
      }
      public Boolean XoaPhieuSuDungDichVuByID(int pSDDV_ID)
      {
          Boolean kq = false;
          string str = "delete from PhieuSuDungDichVu where PSDDV_ID="+pSDDV_ID;
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(str);
          if (i != 0)
              kq = true;
          return kq;
      }
      public Boolean XoaPhieuSuDungDichVuByMaPhieuSuDungDichVu(string maPhieuSuDungDichVu)
      {
          Boolean kq = false;
          string str = "delete from PhieuSuDungDichVu where MaPhieuSuDungDichVu='" + maPhieuSuDungDichVu+"'";
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(str);
          if (i != 0)
              kq = true;
          return kq;
      }

    }
}
